package com.xckj.cycling.activity;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;

import com.xckj.cycling.R;
import com.xckj.cycling.databinding.ActivityPerfectRegisterBinding;
import com.xckj.cycling.framework.BaseActivity;
import com.xckj.cycling.view.dialog.DateDialog;
import com.xckj.cycling.view.dialog.GenderDialog;

import java.util.Calendar;

/**
 * @Author: ChenYao
 * @Describe: 完善注册
 * @Date: 2023/3/1 23:42
 */
public class PerfectRegisterActivity extends BaseActivity<ActivityPerfectRegisterBinding> {


    @Override
    protected int loadLayout() {
        return R.layout.activity_perfect_register;
    }

    @Override
    protected void setTitle() {
        super.setTitle();
        dataBinding.viewTitle.tvTitle.setText(getString(R.string.perfect_register));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        dataBinding.rlSelectAge.setOnClickListener(this);
        dataBinding.rlSelectGender.setOnClickListener(this);
        dataBinding.btnSubmit.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_select_age:
                showSelectAgeDialog();
                break;
            case R.id.rl_select_gender:
                showSelectGenderDialog();
                break;
            case R.id.btn_submit:
                submit();
                break;
            default:
                break;
        }
    }

    private void showSelectAgeDialog() {
        new DateDialog.Builder(this).setListener(new DateDialog.MyOnClickListener() {
            @SuppressLint("DefaultLocale")
            @Override
            public void onSelected(int year, int month, int day) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month - 1);
                calendar.set(Calendar.DAY_OF_MONTH, day);
                long systemDate = System.currentTimeMillis();
                long birthdayDate = calendar.getTimeInMillis();
                int age = (int) ((systemDate - birthdayDate) / (365L * 24 * 3600 * 1000));
                dataBinding.tvAge.setText(String.format("%d岁", age));

            }
        }).show();
    }

    private void showSelectGenderDialog() {
        new GenderDialog(this).setSelectedGenderClickListener(new GenderDialog.SelectedGenderOnClickListener() {
            @Override
            public void onSelectedGender(int gender) {
                if (1 == gender) {
                    dataBinding.tvGender.setText("男");
                } else if (2 == gender) {
                    dataBinding.tvGender.setText("女");
                } else {
                    dataBinding.tvGender.setText("其他");
                }

            }
        }).build().show();
    }


    private void submit() {
        String name = dataBinding.etName.getText().toString();
        String age = dataBinding.tvAge.getText().toString();
        String gender = dataBinding.tvGender.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showMessage(getString(R.string.name_hint));
            return;
        }
        if (TextUtils.isEmpty(age)) {
            showMessage(getString(R.string.age_hint));
            return;
        }
        if (TextUtils.isEmpty(gender)) {
            showMessage(getString(R.string.sex_hint));
            return;
        }
        submit(name, age, gender);
    }

    private void submit(String name, String age, String gender) {

    }
}
